var app = angular.module('myApp', []);
function baseEX ($scope, $http, ajaxURL, pageNowName) {
  $scope.pages = {
    data: [],
    count: 0,
    maxPage: "...",
    pageNow: 1,
  };
  $scope.getPage = function (pageNow) {
    pageNow = pageNow ? pageNow : 1;
    $http.post(ajaxURL.getPage, { page: pageNow, rows: 10 }).then(function success (rsp) {
      if (rsp.data.status == 'success') {
        $scope.pages = rsp.data;
      }
    }, function err (rsp) {
    });
  }
  $scope.getPage();

  $scope.del = function (status, id, $event) {
    $event.stopPropagation();
    zeroModal.confirm({
      content: '操作提示',
      contentDetail: '是否确认删除吗？',
      okFn: function () {
        rqs_data = {
          status: status,
          id: id,
          page: $scope.pages.pageNow
        };
        var loading = zeroModal.loading(4);
        $http.post("/file/del", rqs_data).then(function success (rsp) {
          zeroModal.close(loading);
          if (rsp.data.status == 'success') {
            $scope.pages = rsp.data;
          }
        }, function err (rsp) {
          zeroModal.close(loading);
        });
      },
      cancelFn: function () {
      }
    });
  }

  $scope.sendFile = function (id, $event) {
    rqs_data = {
      MD5: $scope.File.MD5.toLowerCase(),
      SHA256: $scope.File.SHA256.toLowerCase(),
      status: $scope.File.status,
      page: $scope.pages.pageNow
    };
    var loading = zeroModal.loading(4);
    $http.post("/file/addfile", rqs_data).then(function success (rsp) {
      zeroModal.close(loading);
      console.log(rsp);
      if (rsp.data.status == 'success') {
        zeroModal.success('添加成功');
        $scope.pages = rsp.data;
      } else {
        zeroModal.error(rsp.data.errorMessage);
      }
    }, function err (rsp) {
      zeroModal.close(loading);
    });
  }

  $scope.add = function (type) {
    var W = $(".content").width() * 0.5;
    W = W > 480 ? W : 480;
    var H = (W / 4) * 3;
    var box = null;
    $scope.File = {
      MD5: '',
      SHA256: '',
      status: type
    }

    var titles = {
      '3': '添加文件白名单',
      '4': '添加文件黑名单',
    };

    var hideenBox = null;
    var newFile = null;
    console.log(type);

    switch (type) {
      case '3':
        hideenBox = hideenBox_white;
        newFile = newFile_white;
        break;
      case '4':
        hideenBox = hideenBox_black;
        newFile = newFile_black;
        break;
      default:
        break;
    }

    box = zeroModal.show({
      title: titles[type],
      content: newFile,
      width: W + "px",
      height: H + "px",
      ok: true,
      cancel: true,
      okFn: function () {
        var MD5 = $scope.File.MD5;
        var flag = true;

        if (MD5 == null || MD5.length == 0) {
          $scope.File.MD5 = "";
          flag = false;
          $scope.MD5error = true;
        } else if (!/^[0-9A-Za-z]{32}$/.test(MD5)) {
          $scope.MD5error = true;
          flag = false;
        } else {
          $scope.MD5error = false;
        }

        $scope.$apply();
        if (!flag) {
          return false;
        }
        $scope.sendFile();
      },
      onCleanup: function () {
        hideenBox.appendChild(newFile);
      }
    });
  }
}

function IpPort ($scope, $http, ajaxURL, pageNowName) {
  $scope.pages = {
    data: [],
    count: 0,
    maxPage: "...",
    pageNow: 1,
  };
  $scope.getPage = function (pageNow) {
    pageNow = pageNow ? pageNow : 1;
    $http.post(ajaxURL.getPage, { page: pageNow, rows: 10 }).then(function success (rsp) {
      if (rsp.data.status == 'success') {
        $scope.pages = rsp.data;
      }
    }, function err (rsp) {
    });
  }
  $scope.getPage();
  // 添加
  $scope.addIP = function (data) {
    let params = {
      ip: data.IP,
      port: data.port * 1,
    }
    var loading = zeroModal.loading(4);
    $http.post("/ipport/ip-port-add", params).then(function success (rsp) {
      zeroModal.close(loading);
      console.log(rsp.data);
      if (rsp.data.status == 'success') {
        zeroModal.success('添加成功');
        $scope.getPage();
      } else {
        zeroModal.error(rsp.data.errorMessage);
      }
    }, function err (rsp) {
      zeroModal.close(loading);
    });
  }
  // 删除
  $scope.del = function (id, $event) {
    console.log(id);
    console.log($event);
    $event.stopPropagation();
    zeroModal.confirm({
      content: '操作提示',
      contentDetail: '是否确认删除吗？',
      okFn: function () {
        var loading = zeroModal.loading(4);
        $http.get("/ipport/ip-port-del?id=" + id).then(function success (rsp) {
          zeroModal.close(loading);
          if (rsp.data.status == 'success') {
            zeroModal.success('删除成功');
            $scope.getPage();
          }
        }, function err (rsp) {
          zeroModal.close(loading);
        });
      },
      cancelFn: function () {
      }
    });
  }
  $scope.add = function (type) {
    console.log(2222);
    var W = $(".content").width() * 0.5;
    W = W > 480 ? W : 480;
    var H = (W / 4) * 3;
    var box = null;
    $scope.File = {
      IP: '',
      port: '',
    }
    $scope.IPerror = false;
    $scope.Porterror = false;
    var hideenBox = null;
    var newFile = null;
    hideenBox = hideenBox_ip;
    newFile = newFile_ip;
    box = zeroModal.show({
      title: 'IP/端口黑名单',
      content: newFile,
      width: W + "px",
      height: H + "px",
      ok: true,
      cancel: true,
      okFn: function () {
        var flag = true;
        var rexIp = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/
        var rexport = /^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/
        console.log($scope.File);
        $scope.IPerror = false;
        $scope.Porterror = false;
        if ($scope.File.IP == '' && $scope.File.port == '') {
          zeroModal.error('IP和端口必须填写一项!');
          $scope.IPerror = true;
          $scope.Porterror = true;
          flag = false;
        } else {
          if (!rexIp.test($scope.File.IP) && $scope.File.IP != '') {
            zeroModal.error('IP格式不正确!');
            flag = false;
            $scope.IPerror = true;
          }
          console.log($scope.File.port * 1);
          if (!rexport.test($scope.File.port * 1)) {
            zeroModal.error('端口格式不正确!');
            flag = false;
            $scope.Porterror = true;
          }
        }
        $scope.$apply();
        if (!flag) {
          return false;
        }
        $scope.addIP($scope.File);
      },
      onCleanup: function () {
        hideenBox.appendChild(newFile);
      }
    });
  }
}



if (!myApp) {
  var myApp = angular.module('myApp', []);
}

myApp.controller('WhiteCtrl', function ($scope, $http, $filter) {
  baseEX($scope, $http, {
    getPage: "/file/whitepage"
  });
});

myApp.controller('BlackCtrl', function ($scope, $http, $filter) {
  baseEX($scope, $http, {
    getPage: "/file/blackpage"
  });
});
myApp.controller('IPCtrl', function ($scope, $http, $filter) {
  IpPort($scope, $http, {
    getPage: "/ipport/ip-port-page"
  });
});



